package com.vitabuybuy.tv.ui.tencent.liveroom.ui

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.GoodsDetailsDto
import com.vitabuybuy.tv.dto.LiveDteilaDto
import com.vitabuybuy.tv.network.CoinRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import com.vitabuybuy.tv.network.StoreRepository
import com.vitabuybuy.tv.ui.tencent.liveroom.bean.GiftDto
import com.vitabuybuy.tv.ui.tencent.liveroom.bean.GiftListBean
import com.vitabuybuy.tv.ui.tencent.liveroom.bean.LiveGoodsBean
import com.vitabuybuy.tv.ui.tencent.liveroom.repository.LiveRepository
import rxhttp.async

class LiveRoomViewModel : BaseViewModel() {

    val goodsList = MutableLiveData<MutableList<LiveGoodsBean>>()
    val viewQuantity = MutableLiveData<Int>()
    val giftAndCoin = MutableLiveData<GiftDto>()
    val liveDetils = MutableLiveData<LiveDteilaDto>()
    val goodDetails = MutableLiveData<GoodsDetailsDto>()

    fun joinLive(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                LiveRepository.joinLive(liveId).await()
                defUI.msgEvent.postValue(MessageModel(5))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun exitLive(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                LiveRepository.exitLive(liveId).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getViewQuantity(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                viewQuantity.value = LiveRepository.getViewQuantity(liveId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun livePraise(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                LiveRepository.livePraise(liveId).await()
                defUI.msgEvent.postValue(MessageModel(2))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun getGiftList(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                val value1 = LiveRepository.getGiftList(liveId).async(this)
                val value2 = CoinRepository.getMyCoin().async(this)
                giftAndCoin.value = GiftDto(value1.await(), value2.await())
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getGoodsList(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                goodsList.value = LiveRepository.getGoodsList(liveId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun sendGift(liveId: Int?, gift: GiftListBean?, quantity: Int?) {
        rxHttpRequest {
            onRequest = {
                LiveRepository.sendGift(liveId, gift?.id, quantity).await()
                defUI.msgEvent.postValue(MessageModel(3, arg1 = quantity!!, obj = gift))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun collectOrcancel(id: Int?, isFav: Boolean, isExecutor: Boolean) {
        rxHttpRequest {
            onRequest = {
                if (isExecutor) {
                    StoreRepository.collectOrcancelExecutor(id, isFav)
                } else {
                    LiveRepository.collectOrcancelStore(id, isFav).await()
                }
                defUI.msgEvent.postValue(MessageModel(4, obj = isFav))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

//    fun addToCart(goodId: Int?, buyNum: Int) {
//        rxHttpRequest {
//            onRequest = {
//                ShopCenterRepository.addToCart(goodId, buyNum).await()
//                defUI.msgEvent.postValue(MessageModel(6))
//            }
//            onError = {
//                XToast.normal(it.msg)
//            }
//        }
//    }

    fun getLiveDetail(liveId: Int?) {
        rxHttpRequest {
            onRequest = {
                liveDetils.value = LiveRepository.getLiveDetail(liveId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun putOnLiveGoods(it: LiveGoodsBean, liveId: Int?, position: Int) {
        rxHttpRequest {
            onRequest = {
                LiveRepository.openSell(liveId, it.id).await()
                defUI.msgEvent.postValue(MessageModel(1, obj = it, arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun reportAnchor(liveId: Int?, reortString: String) {
        rxHttpRequest {
            onRequest = {
                LiveRepository.reportAnchor(liveId, reortString).await()
                defUI.msgEvent.postValue(MessageModel(100))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
    fun getDetails(goodId: Int?) {
        rxHttpRequest {
            onRequest = {
                goodDetails.value = ShopCenterRepository.getDetails(goodId).await()
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }
}